package cn.xz.zgsg.adapter

import android.widget.TextView
import cn.xz.zgsg.R
import cn.xz.zgsg.activity.Main1Activity
import cn.xz.zgsg.bean.DeviceList
import cn.xz.zgsg.bean.Flash
import cn.xz.zgsg.net.OkHttpUtil
import cn.xz.zgsg.util.LocalHttp
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 *创建基础表适配器*/
class CJListAdapter111(mDevice: DeviceList, type: Int): BaseQuickAdapter<Flash, BaseViewHolder>(R.layout.item_local_list) {

    private var selectPosition = 0

    private var mDevice = mDevice
    private var type = type // 0-分页

    fun getPosition(): Int{
        return selectPosition
    }

    fun setPosition(i: Int){
        selectPosition = i;
        notifyDataSetChanged()
    }

    private var mClick: OnClickListener ?  = null
    interface OnClickListener{
        fun on( item: Flash)
    }

    fun setOnMyClickListener(click: OnClickListener){
        mClick = click
    }

    override fun convert(holder: BaseViewHolder, item: Flash) {
        val mText = holder.getView<TextView>(R.id.channel_item_tv)

        if (item.title.length > 10) {
            mText.textSize = 14f
        } else {
            mText.textSize = 16f
        }
        mText.text = item.title
        mText.isSelected = holder.adapterPosition == selectPosition

        mText.setOnClickListener {
            if (mClick != null){
                mClick!!.on(item)
            }
            if (type == 0){
                val url = "http://" + mDevice.ip +":8030/?act=unity&states=SelectPage&object=${holder.adapterPosition}"
                OkHttpUtil.post(url);
            }else if (type == 3){
                LocalHttp.playLocal(mDevice.ip,holder.adapterPosition)
            } else{
                if (type == 1){
                    Main1Activity.mSInt = holder.adapterPosition
                }
                //重置对应数据
                val url = "http://" + mDevice.ip +":8030/?act=unity&states=SelectVideo&object=${holder.adapterPosition}"
                OkHttpUtil.post(url);
            }
            selectPosition = holder.adapterPosition
            notifyDataSetChanged()
        }

    }
}